<?php
/*
 * (C) Copyright 2013 Torsten Gorath (http://www.blauesdach.de/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Torsten Gorath
 */
require_once $basedir."/inc/adress.php";
$adress = new Adresse();
if(isset($_REQUEST['id'])) {
	$adress = new Adresse($_REQUEST['id']);
}
if(isset($_REQUEST['save'])) {
	$adress->setAnrede($_REQUEST['anrede']);
	$adress->setVorname($_REQUEST['vorname']);
	$adress->setNachname($_REQUEST['nachname']);
    $adress->setNameShow(isset($_REQUEST['name_show'])?1:0);
	$adress->setStrasse($_REQUEST['strasse']);
	$adress->setHausnummer($_REQUEST['hausnummer'], $_REQUEST['zusatz_hausnummer']);
	$adress->setPlz($_REQUEST['plz']);
	$adress->setOrt($_REQUEST['ort']);
    $adress->setAdresseShow(isset($_REQUEST['adresse_show'])?1:0);
	$adress->setTelefon($_REQUEST['telefon']);
    $adress->setTelefonShow(isset($_REQUEST['telefon_show'])?1:0);
	$adress->setTelBeruf($_REQUEST['tel_beruf']);
	$adress->setMobil($_REQUEST['mobil']);
    $adress->setMobilShow(isset($_REQUEST['mobil_show'])?1:0);
	$adress->setEMail($_REQUEST['email']);
    $adress->setEMailShow(isset($_REQUEST['email_show'])?1:0);
    $adress->setEMailSelten(isset($_REQUEST['email_selten'])?1:0);
    $adress->setEMailErlaubt(isset($_REQUEST['email_erlaubt'])?1:0);
	$adress->setIsDeutsch(isset($_REQUEST['deutsch'])?1:0);
    $adress->setBeruf($_REQUEST['beruf']);
	$adress->save();
}
if($action=='edit') {
	$action = 'add';
} else if($action=='delete') {
    $adress->delete();
}
if($action=='add') {
?>
<form id='stammdaten_adress_add_form' method="post" action="/index.php">
	<input type="hidden" name="page" value="<?php echo $menuid;?>" />
	<input type="hidden" name="id" value="<?php echo $adress->getId();?>" />
    <div>
        <label>Anrede:</label>
        <select name="anrede">
            <option value="Familie"<?php echo $adress->getAnrede()=="Familie"?" selected":"";?>>Familie</option>
            <option value="Frau"<?php echo $adress->getAnrede()=="Frau"?" selected":"";?>>Frau</option>
            <option value="Herr"<?php echo $adress->getAnrede()=="Herr"?" selected":"";?>>Herr</option>
        </select>
    </div>
    <div>
        <label>Vorname:</label>
        <input type="text" name="vorname" maxlength="50" value="<?php echo $adress->getVorname();?>" />
    </div>
    <div>
        <label>Nachname:</label>
        <input type="text" name="nachname" maxlength="50" value="<?php echo $adress->getNachname();?>" />
    </div>
    <div>
        <label>Name Ver&ouml;ffentlichen:</label>
        <input type="checkbox" name='name_show' value="true" <?php echo $adress->isNameShow()?"checked":"";?> />
    </div>
    <div>
        <label>Strasse:</label>
        <input type="text" name="strasse" maxlength="100" value="<?php echo $adress->getStrasse();?>" />
    </div>
    <div>
        <label>Hausnummer:</label>
        <input type="number" name="hausnummer" maxlength="4" value="<?php echo $adress->getHausnummer();?>" />
    </div>
    <div>
        <label>Zusatz Hausnummer:</label>
        <input type="text" name="zusatz_hausnummer" maxlength="2" value="<?php echo $adress->getZusatzHausnummer();?>" />
    </div>
    <div>
        <label>PLZ:</label>
        <input type="text" name="plz" maxlength="5" value="<?php echo $adress->getPlz();?>" />
    </div>
    <div>
        <label>Ort:</label>
        <input type="text" name="ort" maxlength="100" value="<?php echo $adress->getOrt();?>" />
    </div>
    <div>
        <label>Adresse Ver&ouml;ffentlichen:</label>
        <input type="checkbox" name='adresse_show' value="true" <?php echo $adress->isAdresseShow()?"checked":"";?> />
    </div>
    <div>
        <label>Telefon:</label>
        <input type="tel" name="telefon" maxlength="25" value="<?php echo $adress->getTelefon();?>" />
    </div>
    <div>
        <label>Telefon Ver&ouml;ffentlichen:</label>
        <input type="checkbox" name='telefon_show' value="true" <?php echo $adress->isTelefonShow()?"checked":"";?> />
    </div>
    <div>
        <label>Telefon Beruf:</label>
        <input type="tel" name="tel_beruf" maxlength="25" value="<?php echo $adress->getTelBeruf();?>" />
    </div>
    <div>
        <label>Mobil:</label>
        <input type="tel" name="mobil" maxlength="25" value="<?php echo $adress->getMobil();?>" />
    </div>
    <div>
        <label>Mobil Ver&ouml;ffentlichen:</label>
        <input type="checkbox" name='mobil_show' value="true" <?php echo $adress->isMobilShow()?"checked":"";?> />
    </div>
    <div>
        <label>EMail:</label>
        <input type="email" name='email' maxlength="255" value="<?php echo $adress->getEMail();?>" />
    </div>
    <div>
        <label>EMail Ver&ouml;ffentlichen:</label>
        <input type="checkbox" name='email_show' value="true" <?php echo $adress->isEMailShow()?"checked":"";?> />
    </div>
    <div>
        <label>EMail wird selten gelesen:</label>
        <input type="checkbox" name='email_selten' value="true" <?php echo $adress->isEMailSelten()?"checked":"";?> />
    </div>
    <div>
        <label>Erlaubt EMail zu senden:</label>
        <input type="checkbox" name='email_erlaubt' value="true" <?php echo $adress->isEMailErlaubt()?"checked":"";?> />
    </div>
    <div>
        <label>Deutsch:</label>
        <input type="checkbox" " name='deutsch' value="true" <?php echo $adress->isDeutsch()?"checked":"";?> />
    </div>
    <div>
        <label>Beruf:</label>
        <input type="text" name='beruf' maxlength="100" value="<?php echo $adress->getBeruf();?>" />
    </div>
    <div>
        <button type="submit" name="save">Speichern</button>
        <button type="submit" name="reset">Abbrechen</button>
    </div>
</form>
<?php
} else {
	$adresses = Adresse::getAll();
?>
<div>
    <a href='?page=stammdaten_adress&action=add'>Neue Adresse</a>
</div>
<div>
    <table border="1" bordercolor="black" cellpadding="0" cellspacing="0" >
        <tr>
            <th>Anrede</th>
            <th>Vorname</th>
            <th>Nachname</th>
            <th>Strasse</th>
            <th>Hausnummer</th>
            <th>Zusatz Hausnr</th>
            <th>PLZ</th>
            <th>Ort</th>
            <th>Telefon</th>
            <th>Tel. Beruf</th>
            <th>Mobil</th>
            <th>EMail</th>
            <th></th>
            <th></th>
            <th></th>
        </tr>
<?php
if($adresses!=NULL) {
	for($i=0;$i<count($adresses);$i++) {
		$value = $adresses[$i];
		$css_class = "even";
		if($i%2==1) {
			$css_class = "odd";
		}
?>
		<tr class="<?php echo $css_class; ?>">
			<td><?php echo $value->getAnrede();?></td>
			<td><?php echo $value->getVorname();?></td>
			<td><?php echo $value->getNachname();?></td>
			<td><?php echo $value->getStrasse();?></td>
			<td><?php echo $value->getHausnummer();?></td>
			<td><?php echo $value->getZusatzHausnummer();?></td>
			<td><?php echo $value->getPlz();?></td>
			<td><?php echo $value->getOrt();?></td>
			<td><?php echo $value->getTelefon();?></td>
			<td><?php echo $value->getTelBeruf();?></td>
			<td><?php echo $value->getMobil();?></td>
			<td><?php echo $value->getEMail();?></td>
			<td><?php echo !$value->isDeutsch()?"X":"";?></td>
			<td><a href="?page=<?php echo $menuid; ?>&action=edit&id=<?php echo $value->getId();?>">edit</a></td>
			<td><a href="?page=<?php echo $menuid; ?>&action=delete&id=<?php echo $value->getId(); ?>">delete</a></td>
		</tr>
<?php
	}
}
?>
    </table>
</div>
<?php    
}
?>